module executor.search.client.SearchUserExecutor;

import msgtrans;
import message.Constants;
import message.MsgDefine;
import google.protobuf;
import client.ImClient;
import std.array;
import command.StartAppCommand;
import std.conv: to;
import hunt.logging;
import core.stdc.string;

@TransportClient(logicClientName)
class SearchUserExecutor : AbstractExecutor!(SearchUserExecutor){

    @MessageId(MESSAGE.CMD_SEARCH_USER_RES)
    void searchUser(TransportContext ctx, MessageBuffer buffer)
    {
      if (buffer is null)
      {
        logError("searchUser  buffer is null !!!!!");
        return ;
      }
      auto res = new SearchUserRES();
      buffer.data.fromProtobuf!SearchUserRES( res);
      TransportSession client = ImClient.getInstance().getImClientByMixId(ImClient.getInstance().idMixSrc(to!ulong(res.fromId), res.src));
      if (client !is null)
      {
        ServerExtend serverExtend ;//= cast(ServerExtend)buffer.extend;
        memcpy(&serverExtend,buffer.extend.ptr,ServerExtend.sizeof);
        ClientExtend clientExtend;
        clientExtend.requestId = serverExtend.requestId;

        ubyte[ClientExtend.sizeof] extend;
        memcpy(extend.ptr,&clientExtend,ClientExtend.sizeof);

        client.send( new MessageBuffer( MESSAGE.CMD_SEARCH_USER_RES,res.toProtobuf.array,extend));
      }
    }

    @MessageId(MESSAGE.CMD_SEARCH_BY_IDS_RES)
    void searchByIds(TransportContext ctx, MessageBuffer buffer)
    {
      if (buffer is null)
      {
        logError("searchByIds  buffer is null !!!!!");
        return ;
      }
      auto res = new SearchByIdsRES();
      buffer.data.fromProtobuf!SearchByIdsRES( res);
      TransportSession client = ImClient.getInstance().getImClientByMixId(ImClient.getInstance().idMixSrc(to!ulong(res.fromId), res.src));
      if (client !is null)
      {
        ServerExtend serverExtend ;//= cast(ServerExtend)buffer.extend;
        memcpy(&serverExtend,buffer.extend.ptr,ServerExtend.sizeof);
        ClientExtend clientExtend;
        clientExtend.requestId = serverExtend.requestId;

        ubyte[ClientExtend.sizeof] extend;
        memcpy(extend.ptr,&clientExtend,ClientExtend.sizeof);

        client.send( new MessageBuffer( MESSAGE.CMD_SEARCH_BY_IDS_RES,res.toProtobuf.array,extend));
      }
    }

    @MessageId(MESSAGE.CMD_SEARCH_BY_ID_RES)
    void searchById(TransportContext ctx, MessageBuffer buffer)
    {
      if (buffer is null)
      {
        logError("searchById  buffer is null !!!!!");
        return ;
      }
      auto res = new SearchByIdRES();
      buffer.data.fromProtobuf!SearchByIdRES( res);
      TransportSession client = ImClient.getInstance().getImClientByMixId(ImClient.getInstance().idMixSrc(to!ulong(res.fromId), res.src));
      if (client !is null)
      {
        ServerExtend serverExtend ;// = cast(ServerExtend)buffer.extend;
        memcpy(&serverExtend,buffer.extend.ptr,ServerExtend.sizeof);
        ClientExtend clientExtend;
        clientExtend.requestId = serverExtend.requestId;

        ubyte[ClientExtend.sizeof] extend;
        memcpy(extend.ptr,&clientExtend,ClientExtend.sizeof);
        client.send( new MessageBuffer( MESSAGE.CMD_SEARCH_BY_ID_RES,res.toProtobuf.array,extend));
      }
    }


    @MessageId(MESSAGE.CMD_GET_UER_BY_QRCODE_RES)
    void getUserByQrCode(TransportContext ctx, MessageBuffer buffer)
    {
      auto res = new GetUserByQrCodeRES();
      buffer.data.fromProtobuf!GetUserByQrCodeRES( res);

      TransportSession client = ImClient.getInstance().getImClientByMixId(ImClient.getInstance().idMixSrc(to!ulong(res.userId), res.src));
      if (client !is null)
      {
        logInfo("getUserByQrCode : %s",res.user.id);
        client.send( new MessageBuffer( MESSAGE.CMD_GET_UER_BY_QRCODE_RES,res.toProtobuf.array));
      }
    }
}

